package com.be.beadmin.quality.controller;

import com.be.beadmin.quality.dto.input.CabinetQualityAndCabinetErrorDto;
import com.be.beadmin.quality.dto.input.CabinetQualityDto;
import com.be.beadmin.quality.entity.CabinetError;
import com.be.beadmin.quality.dto.output.CabinetErrorEx;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.domain.Page;
import com.be.beadmin.common.entity.PageEntity;
import com.be.beadmin.quality.service.CabinetErrorService;
import com.be.beadmin.quality.dto.input.CabinetErrorQueryCriteria;
import com.be.beadmin.quality.dto.input.CabinetErrorDto;
import org.springframework.data.domain.Pageable;
import com.be.beadmin.common.utils.ResultModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.io.IOException;
import java.util.List;
import com.alibaba.excel.EasyExcel;

/**
* @author zty
* @date 2023-07-01
**/
@RestController
public class CabinetErrorController {

    @Autowired
    private  CabinetErrorService cabinetErrorService;


    @PostMapping(value = "/quality/getCabinetErrorByPage")
    public ResultModel getCabinetErrorByPage(@RequestBody PageEntity pageEntity)  {
        return ResultModel.success(cabinetErrorService.getCabinetErrorByPage(pageEntity)) ;
    }

    @PostMapping("/quality/addCabinetError")
    public ResultModel addCabinetError(@RequestBody CabinetErrorDto cabinetErrorDto){
        cabinetErrorService.addCabinetError(cabinetErrorDto);
        return ResultModel.success(ResultModel.ADD);
    }

    @PostMapping("/quality/delCabinetError")
    public ResultModel delCabinetError(@RequestBody CabinetErrorDto cabinetErrorDto){
        cabinetErrorService.delCabinetError(cabinetErrorDto.getId());
        return ResultModel.success(ResultModel.DELETE);
    }


    @PostMapping("/quality/updateCabinetError")
    public ResultModel updateCabinetError(@RequestBody CabinetErrorDto cabinetErrorDto){
        cabinetErrorService.updateCabinetError(cabinetErrorDto);
        return ResultModel.success(ResultModel.UPDATE);
    }


    @PostMapping("/quality/exportCabinetError")
    public void exportCabinetError(HttpServletResponse response ,@RequestBody PageEntity pageEntity) throws IOException {
        Page<CabinetErrorEx> cabinetErrorPage = cabinetErrorService.getCabinetErrorByPage(pageEntity);
        List<CabinetErrorEx> cabinetErrorExes = cabinetErrorPage.getContent();
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("CabinetError接口", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        EasyExcel.write(response.getOutputStream(), CabinetErrorEx.class).sheet("sheet1").doWrite(cabinetErrorExes);
    }

    @ApiOperation(value = "根据大表id获取小表信息")
    @PostMapping("/quality/getCabinetError")
    public ResultModel getCabinetError(@RequestParam String Id){
        cabinetErrorService.getCabinetError(Id);
        return ResultModel.success(cabinetErrorService.getCabinetError(Id));
    }

    @ApiOperation(value = "实时获取接线错误数量")
    @PostMapping("/quality/countCabinetError")
    public ResultModel countCabinetError(@RequestBody CabinetQualityAndCabinetErrorDto cabinetQualityAndCabinetErrorDto){
        cabinetErrorService.countCabinetError(cabinetQualityAndCabinetErrorDto);
        return ResultModel.success(cabinetErrorService.countCabinetError(cabinetQualityAndCabinetErrorDto));
    }
}